//------------------------------------------------------------------------------ 
//      Copyright (c) 2004-2008, Be Nanashi-San 
// 
//      Permission is hereby granted, free of charge, to any person obtaining a 
//      copy of this software and associated documentation files (the "Software"), 
//      to deal in the Software without restriction, including without limitation 
//      the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//      and/or sell copies of the Software, and to permit persons to whom the 
//      Software is furnished to do so, subject to the following conditions: 
// 
//      The above copyright notice and this permission notice shall be included in 
//      all copies or substantial portions of the Software. 
// 
//      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//      FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//      DEALINGS IN THE SOFTWARE. 
// 
//      File Name:	FindWindow.cpp
//      Author: 	Be Nanashi-San 
//      Description:	 
//------------------------------------------------------------------------------

#include <Button.h>
#include <RadioButton.h>
#include <TextControl.h>

#include "2chPositive.h"
#include "FindWindow.h"
#include "messages.h"
#include "setting.h"
#include "Util.h"


static const unsigned int kOKBUTTON = 'kOKB';
static const unsigned int kFINDTEXT = 'kFTT';
static const unsigned int kRADIO1 = 'kRB1'; 
static const unsigned int kRADIO2 = 'kRB2'; 
static const unsigned int kRADIO3 = 'kRB3'; 

//TODO:大文字小文字、半角全角を区別しない検索。
////////////////////////////////////////////////////////////////////////////////
// コンストラクタ
//
//
////////////////////////////////////////////////////////////////////////////////
FindWindow::FindWindow()
:Dialog(IEtoBRect(300,200,224,100), "Find")
,fFindTarget(FIND_INSIDE_THREAD)
{
	BView * contentView = new BView(this->Bounds(), "ContentView", 0, 0);
	contentView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
	this->AddChild(contentView);
	
	fFindControl = new BTextControl(IEtoBRect(10,9,200,21), "input", NULL, NULL, NULL);
	fFindControl->SetDivider(0.0);
	contentView->AddChild(fFindControl);
	BButton* aButton = MakeButton(IEtoBRect(143,62,75,33), "OK", kOKBUTTON, contentView );
	this->SetDefaultButton(aButton);
	
	BRadioButton* aRadioButton1 = MakeRadioButton(IEtoBRect(18,37,51,18),  "", "スレ", kRADIO1, contentView);
	aRadioButton1->SetValue(1);
	MakeRadioButton(IEtoBRect(18,58,75,18),  "", "スレ一覧", kRADIO2, contentView);
	MakeRadioButton(IEtoBRect(18,81,63,18),  "", "板一覧", kRADIO3, contentView);
	fFindControl->MakeFocus(true);
}
////////////////////////////////////////////////////////////////////////////////
// DoFind
//
////////////////////////////////////////////////////////////////////////////////
void
FindWindow::DoFind()
{
	if(!fFindControl) return;
	const char* findText = fFindControl->Text();
	if(!findText || !strlen(findText)) return;

	this->Hide();
	BMessage findmsg(kMSG_FIND);
	findmsg.AddString("findthis", findText);
	findmsg.AddInt32("targetview", fFindTarget);
	be_app_messenger.SendMessage(&findmsg);
}
////////////////////////////////////////////////////////////////////////////////
// MessageReceived
//
//
////////////////////////////////////////////////////////////////////////////////
void
FindWindow::MessageReceived(BMessage *msg)
{
	switch (msg->what){
		case kOKBUTTON:
//		case kFINDTEXT:
			this->DoFind();
			break;
		case kRADIO1:
			fFindTarget = FIND_INSIDE_THREAD;
			break;
		case kRADIO2:
			fFindTarget = FIND_THREAD_LIST;
			break;
		case kRADIO3:
			//zeta 2 スレ@710, 713のBe名無しさんより。(3->2)
			fFindTarget = FIND_BBS_LIST;
			break;
		default:
			BWindow::MessageReceived (msg);
			break;
	}
}
